﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;


namespace Nirvana
{
    /// <summary>
    /// 调节环境光效果
    /// </summary>
    [AddComponentMenu("Nirvana/Render/Camera Environment Lighting")]
    [RequireComponent(typeof(Camera))]
    public class CameraEnvLighting : MonoBehaviour
    {
        [SerializeField]
        private DefaultReflectionMode reflectionMode = DefaultReflectionMode.Custom;

        [SerializeField]
        private AmbientMode ambientMode = AmbientMode.Trilight;
        [SerializeField]
        private float ambientIntensity;
        [SerializeField]
        private Color ambientLight;
        [SerializeField]
        private Color ambientSkyColor;
        [SerializeField]
        private Color ambientEquatorColor;
        [SerializeField]
        private Color ambientGroundColor;
        [SerializeField]
        private Cubemap customReflection;
        [SerializeField]
        private float reflectionIntensity = 1f;
        [SerializeField]
        private bool fog;
        private DefaultReflectionMode _refmode;
        private AmbientMode _ambientmode;
        private float _ambientIntensity;
        private Color _ambientLight;
        private Color _ambientSkyColor;
        private Color _ambientEquatorColor;
        private Color _ambientGroundColor;
        private Cubemap _customReflection;
        private float _reflectionIntensity = 1f;
        private bool _fog;


        public AmbientMode AmbientMode
        {
            get
            {
                return this.ambientMode;
            }
            set
            {
                this.ambientMode = value;
            }
        }

        public float AmbientIntensity
        {
            get
            {
                return this.ambientIntensity;
            }
            set
            {
                this.ambientIntensity = value;
            }
        }

        public Color AmbientLight
        {
            get
            {
                return this.ambientLight;
            }
            set
            {
                this.ambientLight = value;
            }
        }

        public Color AmbientSkyColor
        {
            get
            {
                return this.ambientSkyColor;
            }
            set
            {
                this.ambientSkyColor = value;
            }
        }

        public Color AmbientEquatorColor
        {
            get
            {
                return this.ambientEquatorColor;
            }
            set
            {
                this.ambientEquatorColor = value;
            }
        }
        public Color AmbientGroundColor
        {
            get
            {
                return this.ambientGroundColor;
            }
            set
            {
                this.ambientGroundColor = value;
            }
        }

        public Cubemap CustomReflection
        {
            get
            {
                return this.customReflection;
            }
            set
            {
                this.customReflection = value;
            }
        }

        public float ReflectionIntensity
        {
            get
            {
                return this.reflectionIntensity;
            }
            set
            {
                this.reflectionIntensity = value;
            }
        }

        public bool Fog
        {
            get
            {
                return this.fog;
            }
            set
            {
                this.fog = value;
            }
        }

        /// <summary>
        /// 在camera开始渲染前
        /// </summary>
        private void OnPreRender()
        {
            this._refmode = RenderSettings.defaultReflectionMode;
            this._ambientmode = RenderSettings.ambientMode;
            this._ambientIntensity = RenderSettings.ambientIntensity;
            this._ambientLight = RenderSettings.ambientLight;
            this._ambientSkyColor = RenderSettings.ambientSkyColor;
            this._ambientEquatorColor = RenderSettings.ambientEquatorColor;
            this._ambientGroundColor = RenderSettings.ambientGroundColor;
            this._customReflection = RenderSettings.customReflection;
            this._reflectionIntensity = RenderSettings.reflectionIntensity;
            this._fog = RenderSettings.fog;

            RenderSettings.defaultReflectionMode = this.reflectionMode;
            RenderSettings.ambientMode = this.ambientMode;
            RenderSettings.ambientIntensity = this.ambientIntensity;
            RenderSettings.ambientLight = this.ambientLight;
            RenderSettings.ambientSkyColor = this.ambientSkyColor;
            RenderSettings.ambientEquatorColor = this.ambientEquatorColor;
            RenderSettings.ambientGroundColor = this.ambientGroundColor;
            RenderSettings.customReflection = this.customReflection;
            RenderSettings.reflectionIntensity = this.reflectionIntensity;
            RenderSettings.fog = this.fog;
        }

        private void OnPostRender()
        {
            RenderSettings.defaultReflectionMode = this._refmode;
            RenderSettings.ambientMode = this._ambientmode;
            RenderSettings.ambientIntensity = this._ambientIntensity;
            RenderSettings.ambientLight = this._ambientLight;
            RenderSettings.ambientSkyColor = this._ambientSkyColor;
            RenderSettings.ambientEquatorColor = this._ambientEquatorColor;
            RenderSettings.ambientGroundColor = this._ambientGroundColor;
            RenderSettings.customReflection = this._customReflection;
            RenderSettings.reflectionIntensity = this._reflectionIntensity;
            RenderSettings.fog = this._fog;
        }
    }
}

